///////////////////////////////////////////////////////////////////////////////
// \author (c) DaLinCaiNiao
//             2022, Shanghai, China
//
// \license The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// \brief delay.c
//
// \version
// v0.0.1: 2022.03.20, Initial version.
///////////////////////////////////////////////////////////////////////////////

#include "stm32f1xx_hal.h"             /* Cortex-M4 processor and core peripherals */

#define US_TICKS				168		// ÿ΢��Ķ�ʱ������ֵ�������MCU������Ƶ�޸ģ�������Ƶ��168MHz
#define MAX_US_PER_PERIOD		(1000 / 2)	// 1�����ڵ����΢�����������System Tick�����޸ģ����ó�System Tick���ڵ�һ�룬��ֹ΢����ʱ�����ɲ�׼ȷ
#define MAX_TICKS_PER_PERIOD	(MAX_US_PER_PERIOD * US_TICKS)	// 1�����ڵ����΢������Ӧ�Ķ�ʱ������ֵ
#define MAX_US_DELAY			4294967295		// uint32_t�������ֵ

void vDelayUs(uint32_t p_dwUs)
{
	uint32_t l_dwReloadValue = SysTick->LOAD;
	uint32_t l_dwUsPeriodNum = p_dwUs / MAX_US_PER_PERIOD;
	uint32_t l_dwUsRemainTicks = (p_dwUs % MAX_US_PER_PERIOD) * US_TICKS;
	uint32_t l_dwDeltTicks = 0;
	uint32_t l_dwCurTicks, l_dwPreTicks, l_dwIntervalTicks, i;

	l_dwPreTicks = SysTick->VAL;
	for(i = 0; i < l_dwUsPeriodNum; i++)
	{
		while(1)
		{
			l_dwCurTicks = SysTick->VAL;
			// Cortex-M4��System Tick�����¼�����
			if(l_dwCurTicks <= l_dwPreTicks)
			{
				l_dwIntervalTicks = l_dwPreTicks - l_dwCurTicks + l_dwDeltTicks;
			}
			else
			{
				l_dwIntervalTicks = l_dwPreTicks + (l_dwReloadValue - l_dwCurTicks + 1) + l_dwDeltTicks;
			}
			if(MAX_TICKS_PER_PERIOD <= l_dwIntervalTicks)
			{
				l_dwPreTicks = SysTick->VAL;
				l_dwDeltTicks = l_dwIntervalTicks - MAX_TICKS_PER_PERIOD;
				break;
			}
		}
	}
	if(0 < l_dwUsRemainTicks)
	{
		while(1)
		{
			l_dwCurTicks = SysTick->VAL;
			if(l_dwCurTicks <= l_dwPreTicks)
			{
				l_dwIntervalTicks = l_dwPreTicks - l_dwCurTicks + l_dwDeltTicks;
			}
			else
			{
				l_dwIntervalTicks = l_dwPreTicks + (l_dwReloadValue - l_dwCurTicks + 1) + l_dwDeltTicks;
			}
			if(l_dwUsRemainTicks <= l_dwIntervalTicks)
			{
				break;
			}
		}
	}
}

void vDelayMs(uint32_t p_dwMs)
{
	uint64_t p_llUs = (uint64_t)p_dwMs * 1000;
	uint32_t p_dwMaxUsNum = p_llUs / MAX_US_DELAY;
	uint32_t p_dwRemainUs = p_llUs % MAX_US_DELAY;
	uint32_t i;

	for(i = 0; i < p_dwMaxUsNum; i++)
	{
		vDelayUs(MAX_US_DELAY);
	}
	if(0 < p_dwRemainUs)
	{
		vDelayUs(p_dwRemainUs);
	}
}

void HAL_Delay(uint32_t Delay)
{
	uint64_t p_llUs = (uint64_t)Delay * 1000;
	uint32_t p_dwMaxUsNum = p_llUs / MAX_US_DELAY;
	uint32_t p_dwRemainUs = p_llUs % MAX_US_DELAY;
	uint32_t i;

	for(i = 0; i < p_dwMaxUsNum; i++)
	{
		vDelayUs(MAX_US_DELAY);
	}
	if(0 < p_dwRemainUs)
	{
		vDelayUs(p_dwRemainUs);
	}
}
